import socket
import struct
# 配置
LISTEN_IP = "192.168.10.100"          # 监听所有本地接口
LISTEN_PORT = 32897             # 目标软件发送的端口
TARGET_IP = "192.168.10.22"        # 目标软件所在 IP（本机）
TARGET_PORT = 32896             # 目标软件监听的端口（如果它也会收包）
# 构造前导部分
header = struct.pack('>I', 0xaabbccdd)          # 4 bytes
zero1 = b'\x00' * 4                             # 4 bytes
middle = struct.pack('>H', 0xab10)              # 2 bytes
zero2 = b'\x00' * 9                             # 9 bytes
padding = b'\x00' * 1024                        # 1024 bytes
zero3 = b'\x00' * 2                             # 2 bytes
footer = b'\xba' + b'\xdd\xcc\xbb\xaa'  # 1 + 4 = 5 bytes

# 拼接最终字节串
reply = header + zero1 + middle + zero2 + padding + zero3+ footer
# 创建 UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((LISTEN_IP, LISTEN_PORT))

print(f"[+] 模拟设备已启动，监听 {LISTEN_IP}:{LISTEN_PORT}")

try:
    while True:
        data, addr = sock.recvfrom(1049)
        print(f"[+] 收到来自 {addr} 的数据: {data.hex()}")

        # 模拟设备回复（你可以根据协议修改）
        sock.sendto(reply, (TARGET_IP, TARGET_PORT))

except KeyboardInterrupt:
    print("\n[!] 用户中断，退出模拟器")
finally:
    sock.close()